package com.warom.sdg.repository;

import android.content.Context;
import com.warom.sdg.dao.GrainSituationDao;
import com.warom.sdg.database.SdgDatabase;
import com.warom.sdg.model.GrainSituationEntity;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 粮情数据仓库
 * 负责粮情数据的数据库操作
 */
public class GrainSituationRepository {

    private final GrainSituationDao grainSituationDao;
    private final ExecutorService executor = Executors.newSingleThreadExecutor();

    public GrainSituationRepository(Context context) {
        SdgDatabase database = SdgDatabase.getInstance(context);
        this.grainSituationDao = database.getGrainSituationDao();
    }

    /**
     * 插入单条粮情数据
     * @param entity 粮情数据实体
     * @param onSuccess 成功回调
     * @param onError 失败回调
     */
    public void insert(GrainSituationEntity entity, Runnable onSuccess, OnError onError) {
        executor.execute(() -> {
            try {
                grainSituationDao.insert(entity);
                if (onSuccess != null) {
                    onSuccess.run();
                }
            } catch (Exception e) {
                if (onError != null) {
                    onError.onError(e.getMessage());
                }
            }
        });
    }

    public interface OnError {
        void onError(String error);
    }
} 